import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
import {Camera, CameraOptions} from '@ionic-native/camera';
import {API} from "../../common/api";
import {AuthProvider} from "../../providers/auth/auth";
import lrz from 'lrz'

@Component({
  selector: 'page-auth-face',
  templateUrl: 'auth-face.html',
})
export class AuthFacePage {

  photo: any = ''
  token: string

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private authProvider: AuthProvider,
              private camera: Camera) {
    this.getToken()
  }

  clickHandle() {
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: false,
      sourceType: this.camera.PictureSourceType.CAMERA
    }
    this.camera.getPicture(options).then((imageData) => {
      // let photo = 'data:image/jpeg;base64,' + imageData;
      lrz('data:image/jpeg;base64,' + imageData, {
        width: 800,
        height: 800,
        quality: 0.85
      }).then((data) => {
        console.log(data)
        this.photo = data.base64
        this.upload(data.file)
      })
    }, (err) => {
      // Handle error
    });
  }

  getToken() {
    this.authProvider.getFaceToken()
  }

  upload(file) {
    this.authProvider.upload(file).then((d) => {
      //上传且识别成功
      console.log(d)
    }).catch(err => {
      //失败
      let code = err.code
      let msg = err.msg

    })
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad AuthFacePage');
  }

}
